var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'cancel_search_ajs.html', controller: ListCustCtrl}).
      when('/add-user', {templateUrl: 'add_new_cancellation.html', controller: AddCustCtrl}).
      when('/deletecust', {templateUrl: 'cancel_search_ajs.html', controller: ListCustCtrl}).
      when('/editcanc/', {templateUrl: 'cancel_search_ajs.html', controller: ListCustCtrl}). 
      when('/updtcust/', {templateUrl: 'cancel_search_ajs.html', controller: ListCustCtrl}). 
      when('/cancel/', {templateUrl: 'cancel_search_ajs.html', controller: ListCustCtrl}). 
      otherwise({redirectTo: '/'});
}]);


app.service('ContactService', function () {
   //  $window.alert("Hello Details  "+cantrndetid);
    this.get = function (cantrndetid,$window) {
        for (i in canceltblmast) {
            if (canceltblmast[i].cantrndetid == cantrndetid) {
                 $window.alert("Edit Cancellation Details  "+cantrndetid+agentid);
                return canceltblmast[i];
            }
        }
 
    };
}
);


function ListCustCtrl($scope, $http,$window,ContactService,$location, $filter)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 15;
    $http.get('canceldata.php').success(function(data) {
    $scope.canceltblmast = data;
	});
    
  //  $scope.canc = $filter("date")(Date.now(), 'yyyy-MM-dd');
   
	$http.get('cancel_search_agentdata.php').success(function(data1) {
    $scope.customermast = data1;
  });
	
	
    
    $scope.edit = function(cantrndetid) {
     // $scope.master = {};
  //  $window.alert("Edit Customer Details  "+cantrndetid);
        //search customerid with given id and update it
      // var i =0;
          for(i in $scope.canceltblmast) {
          // $window.alert("outer value of i is "+i);
            if($scope.canceltblmast[i].cantrndetid == cantrndetid)
            {
               //  $window.alert(" value of cantrndetid is "+i+ "  "+cantrndetid);
               //we use angular.copy() method to create copy of original object
               $scope.newcanceltblmast = angular.copy($scope.canceltblmast[i]);
             //  $window.alert(" value of no of pax is "+No_of_Pax+ "  "+cantrndetid);
            }
        }
         
       /*  $scope.customermast = {};
       
       $scope.reset();
        $scope.reset = function()
            {
               $window.alert("Reset  "+cantrndetid);
              $scope.customermast = angular.copy($scope.master);
            }; */
    };
    
      
   $scope.deletecancel = function(item) {
      //$window.alert("Deletion Flow"+item);
        if (confirm('Do you really want to delete this record?')) 
        {
            $window.alert(item+' Record is being Deleted');
			
			var sql = 'cancel_search_deletedata.php?cantrndetid='+item;
			$window.alert(sql);
            $http.post(sql).success(function(data)
            {
                $window.alert( 'Record for ' + item+ 'got deleted Successfully');
                 $scope.activePath = $location.path('cancel_search_ajs.php#/editcanc/');
                    window.location.href = '#/editcanc/';
            });
                //window.location.href = 'customer_search_ajs.php';
        }
      };
      
    $scope.updtcustdata = function(newcanceltblmast) 
  {   
     $window.alert("Updation of Record for "+newcanceltblmast.Can_Date +","+newcanceltblmast.Pax_Name + ","+ newcanceltblmast.No_of_Pax + ","+ newcanceltblmast.Airline + ","+ newcanceltblmast.Sector + ","+ newcanceltblmast.Net_Rate+ ","+ newcanceltblmast.Refund_Amt+ ","+ newcanceltblmast.Refund_Date+ ","+ newcanceltblmast.Agent_Rate + ","+ newcanceltblmast.Agent_Ref_Amt + ","+ newcanceltblmast.Reference_Date + ","+ newcanceltblmast.Comment + ","+ newcanceltblmast.enteredby );
	 
	 var sql = 'cancel_search_updatedata.php?'+'Can_Date='+newcanceltblmast.Can_Date+'&Pax_Name='+newcanceltblmast.Pax_Name+'&No_of_Pax='+newcanceltblmast.No_of_Pax +'&Airline='+newcanceltblmast.Airline+'&Sector='+newcanceltblmast.Sector+'&Net_Rate='+newcanceltblmast.Net_Rate+'&Refund_Amt='+newcanceltblmast.Refund_Amt+'&Refund_Date='+newcanceltblmast.Refund_Date+'&Agent_Ref_Amt='+newcanceltblmast.Agent_Ref_Amt+'&Agent_Rate='+newcanceltblmast.Agent_Rate+'&Reference_Date='+newcanceltblmast.Reference_Date+'&Comment='+newcanceltblmast.Comment+'&enteredby='+newcanceltblmast.enteredby+'&cantrndetid='+newcanceltblmast.cantrndetid+'&agentid=2';
	 
	// $window.alert(sql);
	 
     $http.post(sql).success(function(data)  
   {
     $window.alert( 'Record for Cancel Tran id '+ newcanceltblmast.cantrndetid  + ' and Cancellation Date ' + newcanceltblmast.Can_Date + 'got Successfully Updated');
     window.location.href = 'cancel_search_ajs.php#/editcanc/';
   });
  };
      
        $scope.cancel= function()
    {
        $scope.activePath = null;
     //$window.alert( 'Cancel ');
      $scope.activePath = $location.path('cancel_search_ajs.php#/editcanc/');
     window.location.href = '#/editcanc/';
    };
      
 }
    
           
     app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
        
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
        
        
 function AddCustCtrl($scope, $http, $location,$window) 
   {
      $scope.master = {};
      $scope.activePath = null;
      $scope.add_new = function(canc, clist,AddNewForm) {
       console.log(canc);
	   $window.alert("Edit Customer Details  "+canc.Pax_Name+" "+clist);
       //$window.alert(" value of cantrndetid is "+Airline+ "  "+Pax_Name);
       // $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';   
		
	var sql = 'cancel_search_insertdata.php?'+'Can_Date='+canc.Can_Date+'&Pax_Name='+canc.Pax_Name+'&No_of_Pax='+canc.No_of_Pax +'&Airline='+canc.Airline+'&Sector='+canc.Sector+'&Net_Rate='+canc.Net_Rate+'&Refund_Amt='+canc.Refund_Amt+'&Refund_Date='+canc.Refund_Date+'&Agent_Ref_Amt='+canc.Agent_Ref_Amt+'&Agent_Rate='+canc.Agent_Rate+'&Reference_Date='+canc.Reference_Date+'&Comment='+canc.Comment+'&agentid='+clist;
	  $window.alert(sql);

        $http.post(sql, canc).success(function()
          {
            $window.alert( 'Cancellation Record got Successfully Added');        
            $scope.reset();
            $scope.activePath = $location.path('cancel_search_ajs.php#/editcanc/');
            window.location.href = '#/editcanc/';
           });
          
      
        $scope.reset = function() 
        {
           $scope.newcanceltblmast = angular.copy($scope.master);
        };
        $scope.reset();
      };
    }


ctrl.directive('handleDestroy', function() {
    return function(scope, tElement, attributes) {        
        scope.$on('$destroy', function() {
            alert("In destroy of:" + scope.todo.text);
        });
    };
});

